package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Consultant;
import com.burgeon.backend.SHOP.entity.Goods;
import com.burgeon.backend.SHOP.entity.StoreName;
import com.burgeon.backend.SHOP.mappers.ConsultantMapper;
import com.burgeon.backend.SHOP.rest.FindConsultant;
import com.burgeon.backend.SHOP.rest.FindConsultantAll;
import com.burgeon.backend.SHOP.service.ConsultantService;
import com.burgeon.backend.WeChat.GetAccesToken;
import com.burgeon.backend.WeChat.WeiXinQRCode;
import com.burgeon.backend.WeChat.Wxacode;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.service.Impl.WechatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ConsultantServiceImpl implements ConsultantService {

    @Autowired
    private ConsultantMapper consultantMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private Wxacode wxacode;
    @Autowired
    private WechatServiceImpl wechatService;
    @Autowired
    private GetAccesToken getAccesToken;

    @Override
    public List<Goods> goodsShow(String token) {
        return consultantMapper.goodsShow(token);
    }

    @Override
    public HashMap showConsultant(String phone, String token, String name) throws Exception {
        HashMap data = new HashMap();
        Info info = GetInfo(token);

        //已绑定专属导购，展示专属导购
        FindConsultant findConsultant = new FindConsultant(info.getUrl(), info.getAccount(), info.getPassword());
        Consultant consultant = findConsultant.getData(phone);
        String qycode = "";
        String head = "";
        if (consultant != null){
            qycode = consultantMapper.findCode(consultant.getId(),token);//二维码
            head = consultantMapper.findHead(consultant.getId(),token);//头像
            consultant.setSex(consultant.getSex());
            consultant.setCode(qycode);
            consultant.setHead(head);
            consultant.setFlag("Y");
            data.put("consultant",consultant);
            return data;
        }

        //未绑定专属导购，展示所有导购
        FindConsultantAll findConsultantAll = new FindConsultantAll(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<Consultant> consultantAll = findConsultantAll.getData(name);

        Map<String, List<Consultant>> storeList = consultantAll.stream().collect(Collectors.groupingBy(Consultant::getStore));//根据店铺进行分组

        ArrayList<StoreName> storeNameList = new ArrayList<>();

        for (Map.Entry<String, List<Consultant>> storeGroup: storeList.entrySet()) {

            List<Consultant> consultantAllList = new ArrayList<>();
            StoreName storeName = new StoreName();
            for (Consultant consultant1:consultantAll) {
                if (storeGroup.getKey().equals(consultant1.getStore())){

                    Consultant consultant2 = new Consultant();
                    consultant2.setId(consultant1.getId());
                    consultant2.setName(consultant1.getName());
                    consultant2.setPhone(consultant1.getPhone());
                    consultant2.setStore(consultant1.getStore());
                    consultant2.setFlag("N");

                    qycode = consultantMapper.findCode(consultant1.getId(),token);//二维码
                    head = consultantMapper.findHead(consultant1.getId(),token);//头像
                    consultant2.setSex(consultant1.getSex());
                    consultant2.setCode(qycode);
                    consultant2.setHead(head);
                    consultantAllList.add(consultant2);

                    /*data.put("name="+storeGroup.getKey()+",item",consultantAllList);*/
                }
            }
            storeName.setName(storeGroup.getKey());
            storeName.setConsultants(consultantAllList);

            storeNameList.add(storeName);
        }
        data.put("storeList",storeNameList);
        return data;
    }

    @Override
    public HashMap showQrCode(String token) {
        HashMap data = new HashMap();

        String accessToken = wechatService.GetAccesToken(token);//公众号的
        
        String ticket = wxacode.createForeverStrTicket(accessToken, token);
        String qrCodeLink = wxacode.showQrcode(ticket);

        System.err.println("qrCodeLink=="+qrCodeLink);
        data.put("qrCodeLink",qrCodeLink);

        String accessToken2 = getAccesToken.GetAccesToken(token);//小程序的

        WeiXinQRCode weiXinQRCode = wxacode.showInfo(accessToken2);

        data.put("nickname",weiXinQRCode.getNickname());
        data.put("headimg",weiXinQRCode.getHeadimg());

        System.err.println("headimg=="+weiXinQRCode.getHeadimg());
        return data;
    }

    private Info GetInfo(String token) {
        return accountMapper.GetTokenData(token);
    }
}
